<?php
/**
**推送订单给厦门先行
**2015-11-02
**User BY  PZP
**pm 17：00
**/
class XianXing{	

	public function __construct(){
		$this->inside = new SoapClient(null,array(
			"location" => "http://localhost/open/openService/pft_insideMX.php",
			"uri" => "www.16u.com?ac_16u=16ucom|pw_16u=c33367701511b4f6020ec61ded352059|auth_16u=true"));
	}
	
	public function push_order($ordern){
		
		$pft_order = $this->inside->Order_Globle_Search(
								0,0,0,0,"",
								"","","","","",
								"","",$ordern,"",0,
								"","","",1,0,
								0,1,"","","",
								"","","","","",
								"","","1","","");
		$pft_order_xml =simplexml_load_string($pft_order);		
		$UUaids=(string)$pft_order_xml->Rec->UUaids;
		if($UUaids == 0){
			$aid = (int)$os->Rec->UUaid;
		}else{
			$UUaidss= explode(',',$UUaids);
			$aid = $UUaidss[0];
		}
		$push_arr = array(
			'todo' 		=> 'addorder', //方法名
			'name' 		=> 'fuzhoupiaofutong', //通讯名
			'passwd' 	=> md5('#$%%RF$fdsiji12301#'), //通讯密码，需md5
			'fid' 		=> (int)$pft_order_xml->Rec->UUmid, //会员fid
			'order_id' 	=> (string)$pft_order_xml->Rec->UUordernum, //订单号
			'order_time'=> (string)$pft_order_xml->Rec->UUordertime, //订单时间
			'use_time'	=> (string)$pft_order_xml->Rec->UUbegintime, //游玩日期
			'amount' 	=> (string)$pft_order_xml->Rec->UUtotalmoney / 100 ,//订单总金额
			'price' 	=> (string)$pft_order_xml->Rec->UUtprice / 100,//会员结算单价
			'number'	=> (int)$pft_order_xml->Rec->UUtnum,//数量
			'uuid' 		=> (int)$pft_order_xml->Rec->UUlid,//景区ID
			'pid' 		=> (int)$pft_order_xml->Rec->UUpid,//产品ID
			'title' 	=> (string)$pft_order_xml->Rec->UUttitle,//门票名称
			'aid' 		=> $aid,//供应商ID
			'tid' 		=> (int)$pft_order_xml->Rec->UUtid //门票ID
		);
		$result = $this->xx_post($push_arr);
		$this->logfile(json_encode($push_arr)."|".$result['code']."|".$result['msg'],'push_result');
		return $result['code'];
	}
	
	public function xx_post($query){
		ksort($query);
		$query_str = http_build_query($query);
		$query['sign']=md5($query_str);

		$jsonStr = json_encode($query); //传json数据
		$url = 'http://wx.16u.com/home/pft/'; //url

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_POST, true );
		curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($ch, CURLOPT_SSLVERSION, 1);
		$res = curl_exec($ch);
		$res  = json_decode(base64_decode($res),true);
		// print_r($res['code']);
		return $res;
	}
	
	public function logfile($word,$path){
		$filepath= '/var/www/html/new/d/logs/api/PustOrder_Xianxing/'.$path.'.txt';
		if(file_exists($filepath)){
			$word = date("Y-m-d H:i:s")."|".$word;
			// $word = mb_convert_encoding($word, 'GBK', 'UTF-8');
			$maxsize  = 1024*1024*10;
			$filesize = filesize($filepath);
			$filetype = end(explode(".",$filepath));
			$filename = substr($filepath,0,strpos($filepath,'.'));
			if($filesize>$maxsize)
			{
				rename($filepath, $filename.'_'.date('YmdHis').'.'.$filetype);
				$handler=fopen($filepath,'w');
				flock($handler, LOCK_EX) ;
				fwrite($handler,"\n".$word);
				flock($handler, LOCK_UN);
				fclose($handler);
			}
			else
			{
				$fp = fopen($filepath,"a");
				flock($fp, LOCK_EX);
				fwrite($fp,$word."\n");
				flock($fp, LOCK_UN);
				fclose($fp);
			}
		}else{
			$fp = fopen($filepath,"a");
			flock($fp, LOCK_EX);
			fwrite($fp,date('Y-m-d H:i:s')."|".$word."\n");
			flock($fp, LOCK_UN);
			fclose($fp);
		}
	}
}
// $xx = new XianXing();
// $push_order = $xx->push_order();
?>